<?php
include "Includes/solotrn_sesion.php";
include "Includes/soloFun_Generales.php";
require_once "config.php";
require_once "Includes/conexion.class.php";

//'----
//'Ingresa aqui cuando da submit en la forma para modificar o eliminar definitivo
$cod_tramite = $_REQUEST["CmbTramite"];
$dpto1 = $_REQUEST["CmbDpto1"];
$dpto2 = $_REQUEST["CmbDpto2"];
$dpto3 = $_REQUEST["CmbDpto3"];
$dpto4 = $_REQUEST["CmbDpto4"];
$dpto5 = $_REQUEST["CmbDpto5"];

if ($dpto2 != "") {
    $tabla = "tb_flujo";
    switch ($_REQUEST["opc"]) {

    //===============================
    //MODIFICAR
    //===============================
        case "M":
            $tipo = "UPDATE";
            $strSql="$tipo $tabla set ";
            $strSql .= "cod_dep1 = $dpto1";
            if($dpto2 != ""){
                $strSql .= ", cod_dep2 = $dpto2";
                if($dpto3 != ""){
                    $strSql .= ", cod_dep3 = $dpto3";
                    if($dpto4 != ""){
                        $strSql .= ", cod_dep4 = $dpto4";
                        if($dpto5 != ""){
                            $strSql .= ", cod_dep5 = $dpto5";
                        }
                    }
                }
            }
            $strSql .= " where cod_tramite = " . $cod_tramite;
            break;
    //         '===============================
    //         'ELIMINAR
    //         '===============================
        case "E":
            $tipo = "DELETE";
            $strSql="$tipo from $tabla";
            $strSql .= " where cod_tramite = $cod_tramite";

    //         '===============================
    //         'INGRESAR
    //         '===============================
            break;
        default:
            //revisar si ya se encuentra ingresado un flujo para el trámite seleccionado
            $tipo = "INSERT";
            $strSql = "select count(*) from tb_flujo where cod_tramite = $cod_tramite";
            $num_tramites = SoloFun_ExecSql($strSql, $Arreglo);
            if($num_tramites[0] == 0){
                $columnas = "cod_tramite, cod_dep1";
                $values = $cod_tramite . ",";
                $values .= $dpto1;


                if($dpto2 != "" && $dpto2 != $dpto1){
                    $columnas .= ", cod_dep2";
                    $values .= ", ".$dpto2;
                    if($dpto3 != "" && $dpto3 != $dpto2 && $dpto3 != $dpto1){
                        $columnas .= ", cod_dep3";
                        $values .= ", ".$dpto3;
                        if($dpto4 != ""){
                            $columnas .= ", cod_dep4";
                            $values .= ", ".$dpto4;
                            if($dpto5 != ""){
                                $columnas .= ", cod_dep5";
                                $values .= ", ".$dpto5;
                            }
                        }
                    }
                }
                //$strSql .= ")";
            }
            $header ="$tipo into $tabla ($columnas) values (";
            $strSql = $header.$values.")";
            break;
    }

    $arrQueries = array();
    array_push($arrQueries, $strSql);
    array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));
    $oConexion = new Conexion($WORKFLOW["motor_base"], $WORKFLOW["db_usuario"], $WORKFLOW["db_password"],
            $WORKFLOW["basedatos"], $WORKFLOW["servidor"], $WORKFLOW["flags"], $WORKFLOW["encoding"]);
//echo $strSql;
    //if(!$oConexion->EjecutarQuery($strSql)) {
    if(!$oConexion->EjecutarTransaccion($arrQueries)) {
        echo $strSql;
        echo $oConexion->getErrorMessage();
        $strMsg="solotrn_AdministrarProcesos.php?SoloError=1&CmbTramite=$cod_tramite&CmbDpto1=$dpto1&CmbDpto2=$dpto2&CmbDpto3=$dpto3&CmbDpto4=$dpto4&CmbDpto5=$dpto5&opc=3";
    }else {
        $strMsg="solotrn_AdministrarProcesos.php?SoloError=0&CmbTramite=$cod_tramite&CmbDpto1=$dpto1&CmbDpto2=$dpto2&CmbDpto3=$dpto3&CmbDpto4=$dpto4&CmbDpto5=$dpto5&opc=3";
    }
    $oConexion->Cerrar();

    header("location: $strMsg");
}

//'-------------------------------------------
//'Ingresa aqui cuando la primera vez ingresa
//'-------------------------------------------

switch ($_REQUEST["opc"]) {
    case "M":
        $boton ="<input type='submit' name='CmdGrabar' value='Modificar' class='SoloStyle_Boton'>";
        $oculto="<input type='hidden' name='opc' value='M'>";
        $titulo="Modificaci&oacute;n de Flujo";
        break;
    case "E":
        $boton ="<input type='submit' name='CmdGrabar' value='Eliminar' class='SoloStyle_Boton'>";
        $oculto="<input type='hidden' name='opc' value='E'>";
        $titulo="Eliminaci&oacute;n de Flujo";
        break;
}

$strSql  = "select id2, des from solotb_nivel2";
$strSql .= " order by des";
SoloFun_ExecSql ($strSql,$arrData2);
?>
<html>
    <head>
        <title>Documentacion</title>
        <link rel="stylesheet" href="SoloStyles/Solostyles.css" type="text/css">
    </head>

    <body topmargin=0 leftmargin=0>
        <!-- Include File="includes/trn_header.asp" -->
        <p align="center" class="SoloStyle_TituloPaginas"><?php echo $titulo;?></p>

        <form name="frmIngreso" action="trn_GrabaAdministrarProcesos.php" method="POST">

            <table border="0" width="70%" align="center">
                <tr>
                    <td class="SoloStyle_Etiqueta">Tr&aacute;mite</td>
                    <td class="SoloStyle_Texto">
                        <?php
                        $strSql="SELECT * FROM tb_flujo WHERE cod_tramite = {$_REQUEST["id1"]};";
                        /*$oConexion = new Conexion($WORKFLOW["motor_base"], $WORKFLOW["db_usuario"], $WORKFLOW["db_password"],
                                $WORKFLOW["basedatos"], $WORKFLOW["servidor"], $WORKFLOW["flags"], $WORKFLOW["encoding"]);
                        $oConexion->EjecutarQuery($strSql);
//echo $strSql;
                        $res = $oConexion->getData();
                        $Des1 = $res[0]["des"];
                        $oConexion->Cerrar();*/
                        SoloFun_ExecSql($strSql, $Arreglo);
                        echo $_REQUEST["id2"];
                        ?>
                        <input type="hidden" maxlength="50" size="50" name="CmbTramite" class="SoloStyle_Texto" value="<?php echo $_REQUEST["id1"];?>"></td>
                </tr>
                <tr>
                    <td class="SoloStyle_Etiqueta">Departamento 1</td>
                    <td class="SoloStyle_Texto">
                        <?php
                        echo $_REQUEST["id4"];
                        ?>
                        <input type="hidden" maxlength="50" size="50" name="CmbDpto1" class="SoloStyle_Texto" value="<?php echo $_REQUEST["id3"];?> "></td>
                </tr>
                <tr>
                    <td class="SoloStyle_Etiqueta">Departamento 2</td>
                    <td class="SoloStyle_Texto">
                        <select name="CmbDpto2" id="select3" class="SoloStyle_Texto" style="width:185px">
                        <?php
                        echo "<option value=''>Seleccione Departamento...</option>";
                        for ($i=0 ;$i < count($arrData2); $i++) {
                            $ind2 = $arrData2[$i][0];
                            $jnd2 = $Arreglo[0][2];
                            if ($jnd2==$ind2) {
                                echo "<option value='" . $arrData2[$i][0] . "' SELECTED >" . $arrData2[$i][1] . "</option>";
                            }else {
                                echo "<option value='" . $arrData2[$i][0] . "'>" . $arrData2[$i][1] . "</option>";
                            }
                        }
                        ?>
                        </select>
                        <input type="hidden" maxlength="50" size="50" name="hidId2" class="SoloStyle_Texto" value="<?php echo $Arreglo[0][2];?>"></td>
                </tr>
                <tr>
                    <td class="SoloStyle_Etiqueta">Departamento 3</td>
                    <td class="SoloStyle_Texto">
                        <select name="CmbDpto3" id="select3" class="SoloStyle_Texto" style="width:185px">
                        <?php
                        echo "<option value=''>Seleccione Departamento...</option>";
                        for ($i=0 ;$i < count($arrData2); $i++) {
                            $ind2 = $arrData2[$i][0];
                            $jnd2 = $Arreglo[0][3];
                            if ($jnd2==$ind2) {
                                echo "<option value='" . $arrData2[$i][0] . "' SELECTED >" . $arrData2[$i][1] . "</option>";
                            }else {
                                echo "<option value='" . $arrData2[$i][0] . "'>" . $arrData2[$i][1] . "</option>";
                            }
                        }
                        ?>
                        </select>
                        <input name="hidId3" type="hidden" class="SoloStyle_Texto" id="CmbDpto3" value="<?php echo $Arreglo[0][3];?>" size="50" maxlength="50">
                    </td>
                </tr>
                <tr>
                    <td class="SoloStyle_Etiqueta">Departamento 4</td>
                    <td class="SoloStyle_Texto">
                        <select name="CmbDpto4" id="select3" class="SoloStyle_Texto" style="width:185px">
                        <?php
                        echo "<option value=''>Seleccione Departamento...</option>";
                        for ($i=0 ;$i < count($arrData2); $i++) {
                            $ind2 = $arrData2[$i][0];
                            $jnd2 = $Arreglo[0][4];
                            if ($jnd2==$ind2) {
                                echo "<option value='" . $arrData2[$i][0] . "' SELECTED >" . $arrData2[$i][1] . "</option>";
                            }else {
                                echo "<option value='" . $arrData2[$i][0] . "'>" . $arrData2[$i][1] . "</option>";
                            }
                        }
                        ?>
                        </select>
                        <input name="hidId4" type="hidden" class="SoloStyle_Texto" id="CmbDpto4" value="<?php echo $Arreglo[0][4];?>" size="50" maxlength="50">
                    </td>
                </tr>
                <tr>
                    <td class="SoloStyle_Etiqueta">Departamento 5</td>
                    <td class="SoloStyle_Texto">
                        <select name="CmbDpto5" id="select3" class="SoloStyle_Texto" style="width:185px">
                        <?php
                        echo "<option value=''>Seleccione Departamento...</option>";
                        for ($i=0 ;$i < count($arrData2); $i++) {
                            $ind2 = $arrData2[$i][0];
                            $jnd2 = $Arreglo[0][5];
                            if ($jnd2==$ind2) {
                                echo "<option value='" . $arrData2[$i][0] . "' SELECTED >" . $arrData2[$i][1] . "</option>";
                            }else {
                                echo "<option value='" . $arrData2[$i][0] . "'>" . $arrData2[$i][1] . "</option>";
                            }
                        }
                        ?>
                        </select>
                        <input name="hidId5" type="hidden" class="SoloStyle_Texto" id="CmbDpto5" value="<?php echo $Arreglo[0][5];?>" size="50" maxlength="50">
                    </td>
                </tr>
            </table>
            <br><center><?php echo $boton;
echo $oculto;?></center>
        </form>

    </body>
</html>